//
//  UIView+CycleLayer.m
//  CycleControlFunction
//
//  Created by ddy on 2018/8/7.
//  Copyright © 2018年 ddy. All rights reserved.
//

#import "UIView+CycleLayer.h"
#import "UITapGestureRecognizer+CycleViewGes.h"
@implementation UIView (CycleLayer)
/**
 * 设置圆角
 */
- (void)cycle_setLayerCornerRadius:(CGFloat)radius{
    self.layer.cornerRadius = radius;
    self.layer.masksToBounds = YES;
}
/**
*  给view切圆角
*  corners : 哪个角
*  cornerRadii : 圆角size
*/
- (instancetype)cycle_cornerByRoundingCorners:(UIRectCorner)corners cornerRadius:(CGFloat)cornerRadius{
    
    UIBezierPath *maskPath = [UIBezierPath bezierPathWithRoundedRect:self.bounds byRoundingCorners:corners cornerRadii:CGSizeMake(cornerRadius, cornerRadius)];
    CAShapeLayer *maskLayer = [[CAShapeLayer alloc] init];
    maskLayer.frame = self.bounds;
    maskLayer.path = maskPath.CGPath;
    self.layer.mask = maskLayer;
    self.layer.contentsScale = [[UIScreen mainScreen] scale];
    return self;
}
/**
 * 设置圆角边框
 */
- (void)cycle_setLayerCornerRadius:(CGFloat)radius andBorderWidth:(CGFloat)width andBorderColor:(UIColor *)borderColor{
    self.layer.cornerRadius = radius;
    self.layer.masksToBounds = YES;
    self.layer.borderColor = borderColor.CGColor;
    self.layer.borderWidth = width;
}
/**
 *  给view切圆角 指定上下左右方向圆角 ，设置边框颜色 纯代码
 *  corners : 哪个角
 *  cornerRadii : 圆角size
*/
- (void)cycle_setupView:(UIView *)view corners:(UIRectCorner)corner cornerRadius:(CGFloat)cornerRadius borderWidth:(CGFloat)width borderColor:(UIColor *)borderColor {
    CAShapeLayer *maskLayer = [CAShapeLayer layer];
    maskLayer.frame = CGRectMake(0, 0, view.frame.size.width, view.frame.size.height);
    //边框
    CAShapeLayer *borderLayer = [CAShapeLayer layer];
    borderLayer.frame = CGRectMake(0, 0, view.frame.size.width, view.frame.size.height);
    borderLayer.lineWidth = width;
    borderLayer.strokeColor = borderColor.CGColor;
    borderLayer.fillColor = [UIColor clearColor].CGColor;
    //指定圆角
    UIBezierPath *bezierPath = [UIBezierPath bezierPathWithRoundedRect:view.bounds byRoundingCorners:corner cornerRadii:CGSizeMake(cornerRadius, cornerRadius)];
    maskLayer.path = bezierPath.CGPath;
    borderLayer.path = bezierPath.CGPath;
    [view.layer insertSublayer:borderLayer atIndex:0];
    [view.layer setMask:maskLayer];
}
/**
 * 设置阴影
 */
- (void)cycle_setShadowColor:(UIColor *)color andShadowOffset:(CGSize)size andShadowOpacity:(CGFloat)opacity{
    //设置阴影的颜色
    self.layer.shadowColor   = color.CGColor;
    //设置阴影的偏移量，如果为正数，则代表为往右边偏移
    self.layer.shadowOffset  = size;
    //设置阴影的透明度(0~1之间，0表示完全透明)
    self.layer.shadowOpacity = opacity;
    //优化
    UIBezierPath *bezierPath = [UIBezierPath bezierPathWithRect:self.frame];
    self.layer.shadowPath = bezierPath.CGPath;
}
/**
 * 画六边形 并点击
 */
- (void)cycle_setSixCornerLayerWithView:(UIImageView *)imageView image:(UIImage *)image cornerColor:(UIColor *)cornerColor cornerMargin:(float)left tapImageViewBlock:(void(^)(UIImageView *imgView))block
{
    
    CGRect hexagnoRect =  CGRectMake(left, left, imageView.bounds.size.width-2*left, imageView.bounds.size.height-2*left);;
    //绘制一个六边形的layer，并复制一个image给他的contents
    CALayer *hexagonLayer = [CALayer layer];
    hexagonLayer.frame = hexagnoRect;
    
    CAShapeLayer * shapLayer = [CAShapeLayer layer];
    shapLayer.lineWidth = 1;
    shapLayer.strokeColor = [UIColor whiteColor].CGColor;
    shapLayer.path = [self c_getSixCGPath:hexagnoRect.size.width];
    hexagonLayer.mask = shapLayer;
    hexagonLayer.contents = (__bridge id _Nullable)(image.CGImage);
    
    
    CAShapeLayer *completeLayer = [CAShapeLayer layer];
    completeLayer.frame = CGRectMake(0, 0, imageView.bounds.size.width, imageView.bounds.size.height);
    [completeLayer addSublayer:hexagonLayer];
    completeLayer.path = [self c_getSixCGPath:imageView.bounds.size.width];
    completeLayer.fillColor = cornerColor.CGColor;
    
    [imageView.layer addSublayer:completeLayer];
    //点击涂层
    imageView.userInteractionEnabled = YES;
    [imageView addGestureRecognizer:[UITapGestureRecognizer cycle_gestureRecognizerWithActionBlock:^(id sender) {
        
        UIImageView *tapImgView = (UIImageView *)sender;
        if (block) {
            block(tapImgView);
        }
    }]];
    
    
}
-(CGPathRef)c_getSixCGPath:(CGFloat)viewWidth{
    UIBezierPath * path = [UIBezierPath bezierPath];
    path.lineWidth = 1.0;
    [[UIColor whiteColor] setStroke];
    //1 左下角第一个点
    [path moveToPoint:CGPointMake((sin(M_1_PI / 180 * 60)) * (viewWidth / 2), (viewWidth / 4))] ;
    //2 中间最顶部的点
    [path addLineToPoint:CGPointMake((viewWidth / 2), 0)];
    //3 右下角第一个点
    [path addLineToPoint:CGPointMake(viewWidth - ((sin(M_1_PI / 180 * 60)) * (viewWidth / 2)), (viewWidth / 4))];
    //4 右下角的第二个点
    [path addLineToPoint:CGPointMake(viewWidth - ((sin(M_1_PI / 180 * 60)) * (viewWidth / 2)), (viewWidth / 2) + (viewWidth / 4))];
    //5 中间最底部的点
    [path addLineToPoint:CGPointMake((viewWidth / 2), viewWidth)];
    //6 左下脚的第二个点
    [path addLineToPoint:CGPointMake((sin(M_1_PI / 180 * 60)) * (viewWidth / 2), (viewWidth / 2) + (viewWidth / 4))];
    
    [path closePath];
    return path.CGPath;
}
/**
 * 画五边形 并点击
 */
- (void)cycle_setFiveCornerLayerWithView:(UIImageView *)imageView image:(UIImage *)image cornerColor:(UIColor *)cornerColor cornerMargin:(float)left tapImageViewBlock:(void(^)(UIImageView *imgView))block
{
    
    CGRect hexagnoRect =  CGRectMake(left, left, imageView.bounds.size.width-2*left, imageView.bounds.size.height-2*left);;
    //绘制一个六边形的layer，并复制一个image给他的contents
    CALayer *hexagonLayer = [CALayer layer];
    hexagonLayer.frame = hexagnoRect;
    
    CAShapeLayer * shapLayer = [CAShapeLayer layer];
    shapLayer.lineWidth = 1;
    shapLayer.strokeColor = [UIColor whiteColor].CGColor;
    shapLayer.path = [self c_getFiveCGPath:hexagnoRect.size.width];
    hexagonLayer.mask = shapLayer;
    hexagonLayer.contents = (__bridge id _Nullable)(image.CGImage);
    
    
    CAShapeLayer *completeLayer = [CAShapeLayer layer];
    completeLayer.frame = CGRectMake(0, 0, imageView.bounds.size.width, imageView.bounds.size.height);
    [completeLayer addSublayer:hexagonLayer];
    completeLayer.path = [self c_getFiveCGPath:imageView.bounds.size.width];
    completeLayer.fillColor = cornerColor.CGColor;
    
    [imageView.layer addSublayer:completeLayer];
    //点击涂层
    imageView.userInteractionEnabled = YES;
    [imageView addGestureRecognizer:[UITapGestureRecognizer cycle_gestureRecognizerWithActionBlock:^(id sender) {
        
        UIImageView *tapImgView = (UIImageView *)sender;
        if (block) {
            block(tapImgView);
        }
    }]];
    
    
}
-(CGPathRef)c_getFiveCGPath:(CGFloat)viewWidth{
    UIBezierPath * path = [UIBezierPath bezierPath];
    path.lineWidth = 1.0;
    [[UIColor whiteColor] setStroke];
    //1 左下角第一个点
    [path moveToPoint:CGPointMake(0, viewWidth*0.4)] ;
    //2 中间最顶部的点
    [path addLineToPoint:CGPointMake((viewWidth / 2), 0)];
    //3 右下角第一个点
    [path addLineToPoint:CGPointMake(viewWidth, viewWidth*0.4)];
    //4 右下角的第二个点
    [path addLineToPoint:CGPointMake(viewWidth*0.75,viewWidth*0.95)];
    //5 中间最底部的点
    //[path addLineToPoint:CGPointMake((viewWidth / 2), viewWidth)];
    //6 左下脚的第二个点
    [path addLineToPoint:CGPointMake(viewWidth*0.25,viewWidth*0.95)];
    
    [path closePath];
    return path.CGPath;
}




@end

